var api = {};
/**
 * 手机登录
 */
api.login = function() {
    var loginType = $('#hidLoginType').val();
    var url = '';
    var request = {savedlogon: true};
    switch (loginType)
    {
        case '0': // 带密码登录
            url = '/openapi/ajaxrequest/run/ml';
            request.loginname = $('#uname').val();
            request.password = $('#password').val();
            break;
        default: // 无密码登录
            url = '/openapi/ajaxrequest/run/mln';
            request.loginname = $('#unamenopwd').val();
            request.smscode = $('#verifynopwd').val();
            break;
    }

    console.log(JSON.stringify(request));
    $.ajax({
        url: url,
        asyn: false,
        dataType: 'json',
        type: 'post',
        data: JSON.stringify(request),
        success: function(retDate) {
            var msg = getMsg(retDate);
            if ($.trim(msg).length > 0) {
                pop(msg, 1, '确定', '', 'popclose()');
            } else {
                location.href = "/h5/main";
            }
        }
    });
}
/**
 * 获取图形验证码
 * 
 * @param {int} keytype 验证码类型[0=手机注册、1=手机无密码登录、2=手机找回密码]
 * @param {function} afterSucceed [成功回调函数]
 */
api.getImgCode = function(keytype, afterSucceed) {
    var url = '/openapi/ajaxrequest/run/mic';
    var request = {
        keytype: keytype,
        loginname: $('#unamenopwd').length > 0 ? $('#unamenopwd').val() : $('#telephone').val()
    };

    console.log(JSON.stringify(request));
    $.ajax({
        url: url,
        asyn: false,
        dataType: 'json',
        type: 'post',
        data: JSON.stringify(request),
        success: function(retDate) {
            var msg = getMsg(retDate);
            if ($.trim(msg).length > 0) {
                pop(msg, 1, '确定', '', 'popclose()');
            } else {
                afterSucceed && afterSucceed();
            }
        }
    });
}
/**
 * 无密码登录获取短信码
 * 
 * @param {function} afterSucceed [成功回调函数]
 */
api.getSms = function(keytype, afterSucceed) {
    var url = '/openapi/ajaxrequest/run/msc';
    var request = {
        keytype: keytype,
        loginname: $('#unamenopwd').length > 0 ? $('#unamenopwd').val() : $('#telephone').val(),
        imgcode: $('#chkCode').length > 0 ? $('#chkCode').val() : $('#verification_code').val()
    };

    console.log(JSON.stringify(request));
    $.ajax({
        url: url,
        asyn: false,
        dataType: 'json',
        type: 'post',
        data: JSON.stringify(request),
        success: function(retDate) {
            var msg = getMsg(retDate);
            if ($.trim(msg).length > 0) {
                $('#divError').html(msg).show();
            } else {
                retDate.data && retDate.data.keycode && pop("短信码：" + retDate.data.keycode, 1, '确定', '', 'popclose()');
                afterSucceed && afterSucceed();
            }
        }
    });
}
/**
 * 注册
 * @param {type} afterSucceed
 * @returns {undefined}
 */
api.register = function(afterSucceed) {
    var url = '/openapi/ajaxrequest/run/ms';
    var request = {
        loginname: $('#telephone').val(),
        smscode: $('#verification_code').val(),
        password: $('#regpwd').val(),
        savedlogon: true
    };

    console.log(JSON.stringify(request));
    $.ajax({
        url: url,
        asyn: false,
        dataType: 'json',
        type: 'post',
        data: JSON.stringify(request),
        success: function(retDate) {
            var msg = getMsg(retDate);
            if ($.trim(msg).length > 0) {
                pop(msg, 1, '确定', '', 'popclose()');
            } else {
                window.location.href = '/h5/main';
                afterSucceed && afterSucceed();
            }
        }
    });
}

// 找回密码
api.findpwd = function(afterSucceed) {
    var url = '/openapi/ajaxrequest/run/mfp';
    var request = {
        loginname: $('#telephone').val(),
        smscode: $('#verification_code').val(),
        password: $('#regpwd').val(),
    };

    console.log(JSON.stringify(request));
    $.ajax({
        url: url,
        asyn: false,
        dataType: 'json',
        type: 'post',
        data: JSON.stringify(request),
        success: function(retDate) {
            var msg = getMsg(retDate);
            if ($.trim(msg).length > 0) {
                pop(msg, 1, '确定', '', 'popclose()');
            } else {
                pop('找回密码成功，请重新登录', 1, '确定', '', 'popclose()');
                afterSucceed && afterSucceed();
            }
        }
    });
}


/**
 * 返回信息
 * 
 * @param {object} res [输出对象]
 * @returns {String}
 */
function getMsg(res) {
    var html = [];
    if (res.error && res.error.message) {
        for (var m in res.error.message) {
            html = html.concat(res.error.message[m]);
        }

        return html.join('<br/>');
    }

    return '';
}
